package com.example.sqliteactivity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.sql.SQLClientInfoException;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createButton =findViewById(R.id.createButton);
        createButton.setOnClickListener(new createButtonListener());
        Button updateButton =findViewById(R.id.updateButton);
        updateButton.setOnClickListener(new updateButtonListener());
        Button insertButton =findViewById(R.id.insertButton);
        insertButton.setOnClickListener(new insertButtonListener());
        Button update =findViewById(R.id.update);
        update.setOnClickListener(new updateListener());
        Button queryButton =findViewById(R.id.queryButton);
        queryButton.setOnClickListener(new queryButtonListener());
    }
    class createButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this,"test_db");
            //创建了一个DatabaseHelper对象
            SQLiteDatabase db =databaseHelper.getReadableDatabase();
            //使用对象的getReadableDatabase或者getWritableDatabase方法创建数据库
        }
    }
    class updateButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this,"test_db",2);
            SQLiteDatabase db=databaseHelper.getReadableDatabase();
        }
    }
    class insertButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            ContentValues contentValues=new ContentValues();
            //生成contentValues对象
            contentValues.put("id",1);
            contentValues.put("name","zhangsan");
            //利用键值对来写入数据
            DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this,"test_db");
            SQLiteDatabase db=databaseHelper.getWritableDatabase();
            db.insert("user",null,contentValues);
        }
    }
    class updateListener implements View.OnClickListener{
        //更新操作等于SQL里的update
        @Override
        public void onClick(View v) {
            DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this,"test_db");
            SQLiteDatabase db=databaseHelper.getWritableDatabase();
            ContentValues contentValues=new ContentValues();
            contentValues.put("name","zhangsanfeng");
            db.update("user",contentValues,"id=?",new String[]{"1"});
            //id后的占位符有几个，new String应该有几位
        }
    }
    class queryButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this,"test_db");
            SQLiteDatabase db =databaseHelper.getReadableDatabase();
            Cursor cursor=db.query("user",new String[]{"id","name"},"id=?",new String[]{"1"},null,null,null);
            while(cursor.moveToNext()){
                String name=cursor.getString(cursor.getColumnIndex("name"));
                System.out.print("query--->"+name);
            }
        }
    }
}
